<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class RegisterPage {

	private $alerter;
	private $item_functions;
	private $access;
	private $db_access;
	private $checker;
	private $creator;
	private $toolbox;
	
	public function __construct() {
		$this->item_functions = new ItemFunctions();
		$this->alerter = new Alerter();
		$this->db_access = new DbAccessor();
		$this->access = new AccessManager();
		$this->checker = new InputChecker();
		$this->creator = new FormCreator();
		$this->toolbox = new Toolbox();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		$this->access->check_access("VIEW");
	}

	function page_inc_execute_action($page, $action) {
		$THISDIR = $page->directory_tree[$page->dir_depth-1];

		switch($action) {
			case "add":
				if ($_SESSION['user_id']) {
					$this->toolbox->endpage("Opnieuw registreren is voor mietjes!");
				}
				break;
			case "edit":
				if (!$_SESSION['user_id']) {
					errorpage(6);
				}
				break;
			default:
				$this->alerter->add_alert("Onbekende actie: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		global $CONFIG;
		$CONTENT = array();
		
		if ($_GET['action'] == "add") {
			$new = 1;
			$CONTENT['title'] = "Registreren";
			$CONTENT['body'] = "<p>Vul de velden in om te registreren. Daarna krijg je een activatiemail in je mailbox.</p><p>Je kan je gegevens later op elk moment inkijken, wijzigen een laten verwijderen, volgens de officiële <a href=/service/24.article>privacywetgeving</a>. Mandragon bewaart je gegevens zo goed mogelijk en geeft ze nooit door aan derden.</p>";
		} else if ($_GET['action'] == "edit") {
			$CONTENT['title'] = "Registratie wijzigen";
			$user = $this->db_access->db_fetch_array(
					$this->db_access->db_do_query(
							$this->db_access->db_query_select(
									array(
											array('*')), 
									array('MEMBER'), 
									"user_id = {$_SESSION['user_id']}")));
			$CONTENT['body'] = "<p>Hier kan je je basisgegevens aanpassen. Je gebruikersnaam kan niet gewijzigd worden.</p>";
		} else {
			$this->toolbox->endpage("selecteer: <a href=\"?action=add\">registreren</a> - <a href=\"?action=edit\">registratie wijzigen</a>");
		}
		
		if ($_POST['submitted'] and !$_POST['new_pwd']) {

			$ok = true;
			if ($new) {
				$ok = $this->checker->check_chars($_POST['nick'], "gebruikersnaam") && 
						$this->checker->check_exists($_POST['nick'], "gebruikersnaam");
			}
			$ok = $this->checker->check_tags($_POST['name'], "echte naam") 
					&& $this->checker->check_exists($_POST['name'], "echte naam") 
				  	&& $this->checker->check_email($_POST['email'], "email") 
					&& $this->checker->check_exists($_POST['email'], "email") 
				  	&& $ok;
			
			if ($new) {
				$usednick = $this->db_access->db_num_rows(
						$this->db_access->db_do_query(
								$this->db_access->db_query_select(
										array(
												array('*')), 
										array('MEMBER'), 
										"nick LIKE '{$_POST['nick']}'")));
				$ok = $this->checker->check_input($usednick == 0, "gebruikersnaam is al in gebruik") 
					&& $this->checker->check_input($_POST['pwd'] == $_POST['pwd2'], "wachtwoorden komen niet overeen") 
					&& $this->checker->check_exists($_POST['pwd'], "wachtwoord") && $ok;
			} else {
				$oldpwd = md5("{$CONFIG['pwd_prefix']}{$_POST['pwd']}{$CONFIG['pwd_suffix']}");
				$ok = $this->checker->check_input($oldpwd == $user['pwd'], "wachtwoorden komen niet overeen") && $ok;

				if ($_POST['pwd3']) {
					$ok = $this->checker->check_input($_POST['pwd3'] == $_POST['pwd2'], "nieuwe wachtwoorden komen niet overeen") 
						&& $this->checker->check_exists($_POST['pwd2'], "nieuw wachtwoord") && $ok;
				}
			}
			
			if ($ok) {
				if ($new) {
					$newpwd = md5("{$CONFIG['pwd_prefix']}{$_POST['pwd']}{$CONFIG['pwd_suffix']}");
					$this->db_access->db_do_query(
							$this->db_access->db_query_insert(
									array('member_since', 'last_visit', 'name', 'email', 'nick', 'pwd', 'private', 'pref_newsletter'), 
									array('NOW(14)', 'NOW(14)', 
										$this->db_access->db_sqlize($_POST['name']), 
										$this->db_access->db_sqlize($_POST['email']), 
										$this->db_access->db_sqlize($_POST['nick']), 
										$this->db_access->db_sqlize($newpwd), 
										$_POST['private']? 1 : 0, $_POST['newsletter']? 1 : 0), 
									"MEMBER"));
					$user_id = $this->db_access->db_insert_id();
					$this->db_access->db_do_query(
							$this->db_access->db_query_insert(
									array('usergroup_id', 'user_id'), 
									array(1, $user_id), "IN_USERGROUP"));
					
					$activationcode = "ACTIV" . md5("{$_POST['nick']}{$_POST['email']}blablabla6");
					$message = "Welkom op mandragon!\n\nJe schreef je zonet in op http://www.mandragon.be . Hier is de link die je nodig hebt om je nieuwe account te activeren: \n\nhttp://www.mandragon.be/service/$user_id.activate?code=$activationcode\n\nHet is de gewoonte dat je je voorstelt op het ledenforum (http://acer/home/profiles). Je hebt nu toegang tot onze fora, downloads en tot de grootste Nederlandstalige community voor rollenspellen en hobby-games.\n\nGroetjes & warme welkomens met slagroom\nhet mandragon-team\n\n\nPS: Kijk ook even naar onze huisregels ajb: http://www.mandragon.be/service/185.article . (Niets om je zorgen om te maken, ze spreken voor zich. Maar je weet maar nooit.)";
					mail("{$_POST['nick']} <{$_POST['email']}>", "Welkom op mandragon.be", $message, "From: {$CONFIG['mail_from']} <{$CONFIG['admin_email']}>\nReply-To: <{$CONFIG['admin_email']}>\nReturn-Path: <{$CONFIG['admin_email']}>");
					
					$this->toolbox->endpage("Je registratie is verwerkt. Normaal gezien zit er nu een e-mail in je mailbox. Om de registratie compleet te maken moet je nu nog de link in de mail aanklikken.");
				} else {
					if ($_POST['pwd3']) {
						$newpwd = md5("{$CONFIG['pwd_prefix']}{$_POST['pwd2']}{$CONFIG['pwd_suffix']}");
						$this->db_access->db_do_query(
								$this->db_access->db_query_update(
										array('member_since', 'last_visit', 'name', 'email', 'pwd', 'private', 'pref_newsletter'), 
										array('member_since', 'last_visit', 
												$this->db_access->db_sqlize($_POST['name']), 
												$this->db_access->db_sqlize($_POST['email']), 
												$this->db_access->db_sqlize($newpwd), $_POST['private']? 1 : 0, $_POST['newsletter']? 1 : 0), 
										"MEMBER", 
										"user_id = {$_SESSION['user_id']}"));
					} else {
						$this->db_access->db_do_query(
								$this->db_access->db_query_update(
										array('member_since', 'last_visit', 'name', 'email', 'private', 'pref_newsletter'), 
										array('member_since', 'last_visit', 
													$this->db_access->db_sqlize($_POST['name']), 
													$this->db_access->db_sqlize($_POST['email']), 
													$_POST['private']? 1 : 0, $_POST['newsletter']? 1 : 0), 
													"MEMBER", 
													"user_id = {$_SESSION['user_id']}"));
					}
					$this->toolbox->endpage("OK, je gegevens zijn aangepast.");
				}
			}
		}
		
		$inputs = array();
		
		$inputs[] = $this->creator->header("Verplichte velden");
		
		$inputs[] = $this->creator->input_field("Echte naam", "name", $user['name']);

		if ($new) {
			$inputs[] = $this->creator->input_field("Gebruikersnaam", "nick", $user['nick']);
		}
		$inputs[] = $this->creator->input_field("E-mail", "email", $user['email']);
		$inputs[] = $this->creator->input_checkbox("Verberg mijn e-mailadres", "private", $user['private']);
		$inputs[] = $this->creator->info("Je e-mailadres wordt afgeschermd voor spambots en niet-ingelogde gebruikers.");
		$inputs[] = $this->creator->input_checkbox("Ja, ik wil de Mandragon-nieuwsbrief ontvangen.", "newsletter", $user['pref_newsletter']);
		
		$inputs[] = $this->creator->input_field($_POST['new_pwd'] ? "Oud wachtwoord" : "Wachtwoord", "pwd", "", "", "", "", "", 1);
		
		if ($_POST['new_pwd']) {
			$inputs[] = $this->creator->input_field("Nieuw wachtwoord", "pwd3", "", "", "", "", "", 1);
		}
		if ($new or $_POST['new_pwd']) {
			$inputs[] = $this->creator->input_field("Bevestig wachtwoord", "pwd2", "", "", "", "", "", 1);
		} else {
			$inputs[] = $this->creator->input_checkbox("Wijzig wachtwoord", "new_pwd");
		}
		
		$CONTENT['registerform'] = $this->creator->create("registerform", array_reverse($inputs));
		$page->content = $CONTENT;
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_body($page);
		print_form($page, $page->content['registerform']);
	}
}

?>